(function(win) {
  if (typeof win.XCF === "undefined") {
    XCF = {};
    XCF.global = {};
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, "");
    };
    // 日期格式化
    Date.prototype.Format = function(fmt) {
      var o = {
        // 月份
        "m+" : this.getMonth() + 1,
        // 日
        "d+" : this.getDate(),
        // 小时
        "h+" : this.getHours(),
        // 分
        "i+" : this.getMinutes(),
        // 秒
        "s+" : this.getSeconds(),
        // 季度
        "q+" : Math.floor((this.getMonth() + 3) / 3),
        // 毫秒
        "S" : this.getMilliseconds()
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for ( var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      return fmt;
    };
  }
  var xcf = {

    config: function (options) {
      XCF.global.staticFileVersion = options.staticFileVersion;
      XCF.global.contextPath = options.contextPath;
      XCF.global.pageSize = options.pageSize;
    },

    isNull: function (value) {
      return value == null || typeof (value) == "undefined";
    },

    isEmpty: function (value) {
      return value === "" || value === null || typeof (value) === "undefined";
    },

    isType: function (type) {
      return function (obj) {
        return {}.toString.call(obj) == "[object " + type + "]";
      };
    },

    isArray: function (obj) {
      return this.isType("Array")(obj);
    },

    // 公共的ajax请求
    ajax: function (url, options) {
      var successFN = function (response) {
        var isErr = response.IsError;
        if (!isErr) {
          options.callback && options.callback(response);
        } else {// 系统异常
          alert(response.Message);
          XCF.util.stopLoading();
        }
      };
      var data = options.data;
      if (options.type == "get") {
        data = $.param(data);
      }
      $.ajax({
        type: options && options.type || "GET",
        url: url,
        data: data,
        dataType: "json",
        success: successFN,
        pagination: false,
        cache: false,
        error: function (xhr, err) {
          var status = xhr.status;
          if (status != "0") {
            // alert("系统异常，请联系管理员!");
          }
          XCF.util.stopLoading();
        }
      });
    },

    callService: function (type, url, param, callback, errorHandler, viewType) {
      $("#loading").show();
      $.ajax({
        type: type.toUpperCase(), url: url, data: param,
        async: false, cache: false, /*dataType:'json',*/
        success: function (data) {
          $("#loading").hide();
          //console.log(data);
          if ("html" == viewType) {
            callback(data);
            return;
          }
          //alert(data.message);
          if (data.ok) {
            if (callback) {
              callback(data);
            }
          } else {
            if (errorHandler) {
              errorHandler(data, {});
            } else {
              alert(data.msg || "系统错误");
            }
          }
        },
        error: function (err, data) {
          $("#loading").hide();
          console.log(err.responseText);
          if (errorHandler) {
            errorHandler(data, err);
          } else {
            alert(data.msg || "系统错误");
          }
        }
      });
    },
    // 加载模板文件
    loadTpl: function (viewURL) {
      var reqURL = XCF.global.contextPath + "/common/tpl";
      var res = $.ajax({
        url: reqURL,
        data: "url=" + viewURL,
        async: false
      });
      return res.responseText;
    }
    ,
    exportExcel: function (type, param) {
      window.open(ctx + "/common/export/exportExcel?exportType=" + type + "&" + param);
    },
    listener: function () {
      //$(".js_btnExportCommon").css("border-width","2px");
      //$(".js_btnExportCommon").css("border-color","white");
      $(".js_btnExportCommon").unbind("click").on("click", function () {
        var type = $(this).attr("exportType");
        var target = $(this).attr("exportTarget");
        var param = $("#" + target).serialize();
        //param = encodeURI(data);
        if (!type || !target) {
          return;
        }
        xcf.exportExcel(type, param);
      });
    }
  }
  //exportUtil.listener();
  XCF = $.extend(XCF,xcf);
  //XCF = $.extend(XCF,exportUtil);
})(this);